package com.feinno.urcs.group.service;

import com.feinno.urcs.group.GroupConfiguration;
import com.feinno.urcs.group.api.GroupSubscriptionManager;
import com.feinno.urcs.group.entity.GroupInfo;
import com.feinno.urcs.group.entity.GroupMemberSubscription;
import com.feinno.urcs.group.entity.GroupSubscribeType;
import com.feinno.urcs.group.pd.task.entity.*;
import com.feinno.urcs.sip.UrcsSipFactory;
import com.feinno.urcs.types.MobileNo;
import org.helium.framework.task.TaskProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sip.address.SipURI;
import javax.sip.address.TelURI;
import java.util.List;

/**
 * Created by wuzhiguo on 15-6-10.
 */
public class PdNotifyMembersHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(PdNotifyMembersHelper.class);

    public static void changeGroupOwnerNotify(TaskProducer<PdChangeGroupOwnerNotifyArgs> task,
                                              GroupSubscriptionManager subManager,
                                              MobileNo exceptMobile,
                                              int groupId,
                                              String groupDomain,
                                              String previousEtag,
                                              String newEtag,
                                              MobileNo oldGroupOwnerMobile,
                                              MobileNo newGroupOwnerMobile) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI oldGroupOwnerUri = UrcsSipFactory.getAddressFactory().createTelURI(oldGroupOwnerMobile.toString());
            TelURI newGroupOwnerUri = UrcsSipFactory.getAddressFactory().createTelURI(newGroupOwnerMobile.toString());

            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdChangeGroupOwnerNotifyArgs args = new PdChangeGroupOwnerNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setOldGroupOwnerUri(oldGroupOwnerUri.toString());
                    args.setNewGroupOwnerUri(newGroupOwnerUri.toString());
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("changeGroupOwnerNotify() Error.", ex);
        }
    }

    public static void deleteGroupNotify(TaskProducer<PdDeleteGroupNotifyArgs> task,
                                         GroupSubscriptionManager subManager,
                                         MobileNo exceptMobile,
                                         int groupId,
                                         String groupDomain,
                                         String previousEtag,
                                         String newEtag) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdDeleteGroupNotifyArgs args = new PdDeleteGroupNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("deleteGroupNotify() Error.", ex);
        }
    }

    public static void groupInfoNotify(TaskProducer<PdGroupInfoNotifyArgs> task,
                                       GroupSubscriptionManager subManager,
                                       MobileNo exceptMobile,
                                       int groupId,
                                       String groupDomain,
                                       String previousEtag,
                                       String newEtag,
                                       GroupInfo groupInfo) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdGroupInfoNotifyArgs args = new PdGroupInfoNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setGroupInfo(groupInfo);
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("groupInfoNotify() Error.", ex);
        }
    }

    // todo 后续判断接受人是支持PD Group才下发
    public static void groupInviteMessage(TaskProducer<PdGroupInviteMessageArgs> task,
                                          int groupId,
                                          String groupDomain,
                                          String groupName,
                                          MobileNo groupOwnerMobile,
                                          String groupConversationId,
                                          MobileNo toMobile,
                                          MobileNo inviterMobile) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI groupOwnerUri = UrcsSipFactory.getAddressFactory().createTelURI(groupOwnerMobile.toString());
            TelURI toUri = UrcsSipFactory.getAddressFactory().createTelURI(toMobile.toString());
            TelURI inviterUri = UrcsSipFactory.getAddressFactory().createTelURI(inviterMobile.toString());

            PdBaseGroupMessageArgs baseGroupMessage = new PdBaseGroupMessageArgs();
            baseGroupMessage.setGroupUri(groupUri.toString());
            baseGroupMessage.setGroupName(groupName);
            baseGroupMessage.setGroupOwnerUri(groupOwnerUri.toString());
            baseGroupMessage.setGroupConversationId(groupConversationId);
            baseGroupMessage.setToUri(toUri.toString());

            PdGroupInviteMessageArgs args = new PdGroupInviteMessageArgs();
            args.setBaseGroupMessage(baseGroupMessage);
            args.setInviterUri(inviterUri.toString());
            task.produce(args);

        } catch (Exception ex) {
            LOGGER.error("groupInviteMessage() Error.", ex);
        }
    }

    // todo 后续判断接受人是支持PD Group才下发
    public static void groupInviteReplyMessage(TaskProducer<PdGroupInviteReplyMessageArgs> task,
                                               int groupId,
                                               String groupDomain,
                                               String groupName,
                                               MobileNo groupOwnerMobile,
                                               String groupConversationId,
                                               MobileNo toMobile,
                                               MobileNo inviteeMobile,
                                               String option) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI groupOwnerUri = UrcsSipFactory.getAddressFactory().createTelURI(groupOwnerMobile.toString());
            TelURI toUri = UrcsSipFactory.getAddressFactory().createTelURI(toMobile.toString());
            TelURI inviteeUri = UrcsSipFactory.getAddressFactory().createTelURI(inviteeMobile.toString());

            PdBaseGroupMessageArgs baseGroupMessage = new PdBaseGroupMessageArgs();
            baseGroupMessage.setGroupUri(groupUri.toString());
            baseGroupMessage.setGroupName(groupName);
            baseGroupMessage.setGroupOwnerUri(groupOwnerUri.toString());
            baseGroupMessage.setGroupConversationId(groupConversationId);
            baseGroupMessage.setToUri(toUri.toString());

            PdGroupInviteReplyMessageArgs args = new PdGroupInviteReplyMessageArgs();
            args.setBaseGroupMessage(baseGroupMessage);
            args.setInviteeUri(inviteeUri.toString());
            args.setOption(option);
            task.produce(args);

        } catch (Exception ex) {
            LOGGER.error("groupInviteReplyMessage() Error.", ex);
        }
    }

    public static void groupInviteReplyNotify(TaskProducer<PdGroupInviteReplyNotifyArgs> task,
                                              GroupSubscriptionManager subManager,
                                              MobileNo exceptMobile,
                                              int groupId,
                                              String groupDomain,
                                              String previousEtag,
                                              String newEtag,
                                              MobileNo groupMemberMobile,
                                              String displayName) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI groupMemberUri = UrcsSipFactory.getAddressFactory().createTelURI(groupMemberMobile.toString());

            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdGroupInviteReplyNotifyArgs args = new PdGroupInviteReplyNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setGroupMemberUri(groupMemberUri.toString());
                    args.setDisplayName(displayName);
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("groupInviteReplyNotify() Error.", ex);
        }
    }

    public static void removeGroupMemberNotify(TaskProducer<PdRemoveGroupMemberNotifyArgs> task,
                                               GroupSubscriptionManager subManager,
                                               MobileNo exceptMobile,
                                               int groupId,
                                               String groupDomain,
                                               String previousEtag,
                                               String newEtag,
                                               MobileNo groupMemberMobile) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI groupMemberUri = UrcsSipFactory.getAddressFactory().createTelURI(groupMemberMobile.toString());

            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdRemoveGroupMemberNotifyArgs args = new PdRemoveGroupMemberNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setGroupMemberUri(groupMemberUri.toString());
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("removeGroupMemberNotify() Error.", ex);
        }
    }

    public static void updateGroupMemberNameNotify(TaskProducer<PdUpdateGroupMemberNameNotifyArgs> task,
                                                   GroupSubscriptionManager subManager,
                                                   MobileNo exceptMobile,
                                                   int groupId,
                                                   String groupDomain,
                                                   String previousEtag,
                                                   String newEtag,
                                                   MobileNo groupMemberMobile,
                                                   String displayName) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            TelURI groupMemberUri = UrcsSipFactory.getAddressFactory().createTelURI(groupMemberMobile.toString());

            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdUpdateGroupMemberNameNotifyArgs args = new PdUpdateGroupMemberNameNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setGroupMemberUri(groupMemberUri.toString());
                    args.setDisplayName(displayName);
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("updateGroupMemberNameNotify() Error.", ex);
        }
    }

    public static void updateGroupNameNotify(TaskProducer<PdUpdateGroupNameNotifyArgs> task,
                                             GroupSubscriptionManager subManager,
                                             MobileNo exceptMobile,
                                             int groupId,
                                             String groupDomain,
                                             String previousEtag,
                                             String newEtag,
                                             String groupName) {
        try {
            SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, groupDomain);
            List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(groupId);
            if (subs != null) {
                for (GroupMemberSubscription sub : subs) {
                    if (sub.getUserMobile().equals(exceptMobile)) {
                        continue;
                    }
                    if (!sub.getSubType().equals(GroupSubscribeType.PD_GROUP)) {
                        continue;
                    }

                    TelURI requestUri = UrcsSipFactory.getAddressFactory().createTelURI(sub.getUserMobile().toString());
                    SipURI fromUri = groupUri;
                    TelURI toUri = requestUri;

                    PdBaseGroupNotifyArgs baseGroupNotify = new PdBaseGroupNotifyArgs();
                    baseGroupNotify.setRequestUri(requestUri.toString());
                    baseGroupNotify.setFromUri(fromUri.toString());
                    baseGroupNotify.setToUri(toUri.toString());
                    baseGroupNotify.setFromTag(sub.getFromTag());
                    baseGroupNotify.setToTag(sub.getToTag());
                    baseGroupNotify.setCallId(sub.getCallId());
                    baseGroupNotify.setCseq(subManager.increateConferenceCseq(groupId, sub.getUserMobile()));
                    baseGroupNotify.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
                    baseGroupNotify.setGroupUri(groupUri.toString());
                    baseGroupNotify.setPreviousEtag(previousEtag);
                    baseGroupNotify.setNewEtag(newEtag);

                    PdUpdateGroupNameNotifyArgs args = new PdUpdateGroupNameNotifyArgs();
                    args.setBaseGroupNotify(baseGroupNotify);
                    args.setGroupName(groupName);
                    task.produce(args);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("updateGroupNameNotify() Error.", ex);
        }
    }
}
